package com.zwg.tingshu.album.receiver;

import com.alibaba.fastjson.JSON;
import com.zwg.tingshu.album.service.AlbumInfoService;
import com.zwg.tingshu.common.constant.RocketMqConstant;
import com.zwg.tingshu.vo.album.TrackStatMqVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = RocketMqConstant.QUEUE_TRACK_STAT_UPDATE,
        selectorExpression = "*",
        consumerGroup = "zwg",
        consumeMode = ConsumeMode.ORDERLY,  //顺序消费，并发度为1
        maxReconsumeTimes = 3
)
public class AlbumReceiver implements RocketMQListener<String> {

    @Autowired
    private AlbumInfoService albumInfoService;
    /**
     * 监听到更新声音统计信息
     * 1.考虑消息幂等性 2.是否需要事务管理
     *
     * @param message
     */
    @Override
    public void onMessage(String message) {
        log.info("[专辑服务]，监听到更新声音统计消息：{}", message);
        TrackStatMqVo mqVo = JSON.parseObject(message, TrackStatMqVo.class);
        albumInfoService.updateTrackStat(mqVo);
    }
}
